package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.dtos.SaveChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
public class WmchannelController {

    @Autowired
    private WmChannelService wmChannelService;

    @GetMapping("/channels")
    public ResponseResult findAll(){
        return wmChannelService.findAll();
    }

    @Autowired
    private WmChannelService channelService;

    @PostMapping("/list")
    private ResponseResult selectAll(@RequestBody ChannelDto dto) {
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();

        if (dto.getName() != null) {
            queryWrapper.like(WmChannel::getName, "%" + dto.getName() + "%");
        }
        Page result = channelService.page(page, queryWrapper);
        List records = result.getRecords();
        long total = result.getTotal();

        ResponseResult responseResult = new PageResponseResult(dto.getPage().intValue(), dto.getSize().intValue(), (int) total);

        responseResult.setData(records);

        return responseResult;
    }

    @PostMapping("/save")
    private boolean saveChannel(@RequestBody SaveChannelDto dto) {
        WmChannel channel = new WmChannel();
        channel.setDescription(dto.getDescription());
        channel.setName(dto.getName());
        channel.setOrd(dto.getOrd());
        channel.setStatus(dto.getStatus());
        channel.setCreatedTime(new Date());
        boolean flag = channelService.save(channel);
        return flag;
    }

    @PostMapping("/update")
    ResponseResult updateChannel(@RequestBody WmChannel channel){
        channel.setCreatedTime(new Date());
        boolean b = channelService.updateById(channel);
        if (b) {
            return ResponseResult.okResult("修改成功");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "修改失败");
        }
    }

    @GetMapping("/del/{id}")
    private ResponseResult deleteChannel(@PathVariable("id") Integer id){
        boolean b = channelService.removeById(id);
        if (b) {
            return ResponseResult.okResult("删除成功");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "删除失败");
        }
    }


    @Autowired
    private WmChannelService channelService;

    @PostMapping("/list")
    private ResponseResult selectAll(@RequestBody ChannelDto dto) {
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();

        if (dto.getName() != null) {
            queryWrapper.like(WmChannel::getName, "%" + dto.getName() + "%");
        }
        Page result = channelService.page(page, queryWrapper);
        List records = result.getRecords();
        long total = result.getTotal();

        ResponseResult responseResult = new PageResponseResult(dto.getPage().intValue(), dto.getSize().intValue(), (int) total);

        responseResult.setData(records);

        return responseResult;
    }

    @PostMapping("/save")
    private boolean saveChannel(@RequestBody SaveChannelDto dto) {
        WmChannel channel = new WmChannel();
        channel.setDescription(dto.getDescription());
        channel.setName(dto.getName());
        channel.setOrd(dto.getOrd());
        channel.setStatus(dto.getStatus());
        channel.setCreatedTime(new Date());
        boolean flag = channelService.save(channel);
        return flag;
    }

    @PostMapping("/update")
    ResponseResult updateChannel(@RequestBody WmChannel channel){
        channel.setCreatedTime(new Date());
        boolean b = channelService.updateById(channel);
        if (b) {
            return ResponseResult.okResult("修改成功");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "修改失败");
        }
    }

    @GetMapping("/del/{id}")
    private ResponseResult deleteChannel(@PathVariable("id") Integer id){
        boolean b = channelService.removeById(id);
        if (b) {
            return ResponseResult.okResult("删除成功");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "删除失败");
        }
    }

}
